
package com.faithbj.device.init;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.faithbj.device.model.entity.Members;
import com.faithbj.device.model.entity.Roles;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-5-26 下午09:05:44
 * 类说明
 */
public class SystemInitListener implements ServletContextListener
{
	Logger logger = LoggerFactory.getLogger(SystemInitListener.class);
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0)
	{
	}

	@Override
	public void contextInitialized(ServletContextEvent sce)
	{
		/*WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());

		SessionFactory sessionFactory = (SessionFactory) appctx.getBean("sessionFactory"); 
		
		Session session = sessionFactory.openSession();
		
		logger.info("system init role begin...");
		
		List<Roles> roleList = (List<Roles>) session.createQuery("from Roles role").list();
		
		for(Roles role : roleList)
		{
			session.delete(role);
		}
		
		session.flush();
		
		Set<Roles> roles = new HashSet<Roles>();
		
		Roles supervisorRole = new Roles();
		supervisorRole.setName("supervisor");
		supervisorRole.setValue("ROLE_SUPERVISOR");
		supervisorRole.setIsSystem(true);
		supervisorRole.setDescription("supervisor");
		supervisorRole.setCreateDate(new Date());
		supervisorRole.setModifyDate(new Date());
		
		Roles adminRole = new Roles();
		adminRole.setName("admin");
		adminRole.setValue("ROLE_ADMIN");
		adminRole.setIsSystem(true);
		adminRole.setDescription("admin");
		adminRole.setCreateDate(new Date());
		adminRole.setModifyDate(new Date());
		
		Roles userRole = new Roles();
		userRole.setName("user");
		userRole.setValue("ROLE_USER");
		userRole.setIsSystem(true);
		userRole.setDescription("user");
		userRole.setCreateDate(new Date());
		userRole.setModifyDate(new Date());
		
		roles.add(supervisorRole);
		roles.add(adminRole);
		roles.add(userRole);
		
		Members member = new Members();
		member.setUsername("super");
		member.setPassword("super");
		member.setCreateDate(new Date());
		member.setCredentialsNonExpired(true);
		member.setAccountNonLocked(true);
		member.setSafeAnswer("");
		member.setSafeQuestion("");
		member.setEmail("rinanjin@gmail.com");
		member.setEnabled(true);
		member.setRegisterIp("localhost");
		member.setTelephone("");
		member.setRoles(roles);
		member.setLoginDate(new Date());
		member.setLoginIp("127.0.0.1");
		
		session.save(supervisorRole);
		session.save(adminRole);
		session.save(userRole);
		session.save(member);
		
		session.flush();
		session.close();*/
		logger.info("system init role complete");
	}
}
